import axios, { AxiosHeaders } from '@ohos/axios'
import ResultT from '../../utils/Result'
import hilog from '@ohos.hilog'

class UserApi{
  private basePath: string = 'http://localhost:8083/user'

  //用户注册（方法名后面指点泛型）
  public register<H>(userAccount: string, password: string, rePassword: string): Promise<ResultT<H>>{
    const model: object = {
      userAccount,
      password,
      rePassword
    }
    return new Promise((resolve, reject) => {
      axios.post(
        this.basePath + '/register',
        model,
      )
        .then(res => {
          let data: ResultT<H> = res.data
          resolve(data)
        })
    })
  }

  //用户登录
  public login<H>(userAccount: string, password: string): Promise<ResultT<H>>{
    hilog.info(1112, 'login', userAccount + password)
    return new Promise((resolve, reject) => {
      axios.get(
        this.basePath + '/login',
        {
          params: {
            userAccount,
            userPassword: password
          }
        }
      )
        .then(res => {
          let data: ResultT<H> = res.data
          hilog.info(1112, 'login', JSON.stringify(data))
          resolve(data as ResultT<H>) // TODO 返回数据用 resolve
        })
        .catch(reason => {
          hilog.info(1112, 'login', "报错："+JSON.stringify(reason))
        })
    })

  }

  //获取自己的基本信息
  public getUserInfo<H>(): Promise<ResultT<H>> {
    return new Promise((resolve, reject) => {
      axios.get(
        this.basePath + "/getSelfInfo"
      )
        .then(res => {
          let data: ResultT<H> = res.data
          resolve(data as ResultT<H>) // TODO 返回数据用 resolve
        })
    })
  }
}

const userApi = new UserApi()
export default userApi as UserApi



















